//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.tm.chargecar.service.impl;

import com.tm.chargecar.dto.BusinessDto;
import com.tm.chargecar.entity.BusinessEntity;
import com.tm.chargecar.entity.BzBsEntity;
import com.tm.chargecar.entity.RecordEntity;
import com.tm.chargecar.enums.ResultEnum;
import com.tm.chargecar.mapper.BusinessMapper;
import com.tm.chargecar.mapper.BzBsMapper;
import com.tm.chargecar.mapper.RecordMapper;
import com.tm.chargecar.result.Result;
import com.tm.chargecar.service.BusinessService;
import java.math.BigDecimal;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper; //商家表
    @Autowired
    private RecordMapper recordMapper;  // 对账表

    @Autowired
    private BzBsMapper bzBsMapper;   // 商家对账中间表

    public BusinessServiceImpl() {
    }

    public Result queryBusiness(Long bsUser) {
        BusinessEntity businessEntity = this.businessMapper.queryBusiness(bsUser);
        return Result.SUCCESS(businessEntity);
    }

    /***
     *
     * 充电完成后 新增中间表
     *
     * @param bzBsEntity
     */
    @Transactional
    public Result addBusiness(BzBsEntity bzBsEntity) {

        //获取当天0点 毫秒值
        long date = System.currentTimeMillis();
        long oneDate = date / 86400000L * 86400000L - (long) TimeZone.getDefault().getRawOffset();
        //查询当天是否有数据
        RecordEntity recordEntity1 = recordMapper.queryRecordUser(bzBsEntity.getDzBsUser(), oneDate);
        if (recordEntity1 == null) {
            RecordEntity recordEntity = new RecordEntity();
            recordEntity.setDuiShouru(bzBsEntity.getDzBsMoney());
            recordEntity.setBsUser(bzBsEntity.getDzBsUser());
            recordEntity.setDuiDate(oneDate);
            recordMapper.addRecord(recordEntity);

            bzBsEntity.setDzBsType(0);
            bzBsEntity.setDzBsDate(oneDate);

            bzBsMapper.addBzBs(bzBsEntity);


        }
        bzBsEntity.setDzBsType(0);
        bzBsEntity.setDzBsDate(oneDate);

        bzBsMapper.addBzBs(bzBsEntity);

    return Result.SUCCESS();
    }

    /**
     * 新增余额
     * @param businessDto
     * @return
     */
    @Override
    public Result putBalance(BusinessDto businessDto) {
        //指定收入商家
        businessDto.setBsUser(123567899l);
        businessMapper.updateBsUser(businessDto);

        return Result.SUCCESS();
    }

    /**
     * 新增余额
     * @param businessDto
     * @return
     */





    /**
     *  商家提现后中间表记录
     * @param bsUser
     * @return
     */
    @Transactional
    public Result updateBusiness(BusinessDto bsUser) {

        float v = bsUser.getWithdrawal().floatValue();
        //判断提现金额是否大于0
        if (v <= 0.0F) {
            return Result.ERROR(ResultEnum.Withdrawal_ERROR);
        } else {
            //查询商家账户信息
            BusinessEntity businessEntity = this.businessMapper.queryBusiness(bsUser.getBsUser());
            //提现金额是否大于现有金额
            if (businessEntity.getBsBalance().compareTo(bsUser.getWithdrawal()) == -1) {
                return Result.ERROR(ResultEnum.Withdrawal_insufficient_ERROR);
            } else {
                //现有金额减去提现金额
                BigDecimal res = businessEntity.getBsBalance().subtract(bsUser.getWithdrawal());
                //剩余金额
                bsUser.setBsBalance(res);
                //修改数据库余额
                businessMapper.updateBsUser(bsUser);

                //获取当天0点 毫秒值
                long date = System.currentTimeMillis();
                long oneDate = date / 86400000L * 86400000L - (long)TimeZone.getDefault().getRawOffset();
                //查询当天是否有数据
                RecordEntity recordEntity1 = recordMapper.queryRecordUser(bsUser.getBsUser(), oneDate);


                //判断是否有当天的记录
                if (recordEntity1  == null) {
                    RecordEntity recordEntity = new RecordEntity();
                    recordEntity.setDuiDate(oneDate);
                    recordEntity.setDuiZhichu(bsUser.getWithdrawal());
                    recordEntity.setBsUser(bsUser.getBsUser());
                    this.recordMapper.addRecord(recordEntity);

                    BzBsEntity bzBsEntity=new BzBsEntity();
                    bzBsEntity.setDzBsUser(bsUser.getBsUser());
                    bzBsEntity.setDzBsType(1);
                    bzBsEntity.setDzBsDate(oneDate);
                    bzBsEntity.setDzBsMoney(bsUser.getWithdrawal());
                    bzBsMapper.addBzBs(bzBsEntity);

                    return Result.SUCCESS(ResultEnum.Withdrawal_SUCCESS);
                } else {
                    //新增中间表
                    BzBsEntity bzBsEntity=new BzBsEntity();
                    bzBsEntity.setDzBsUser(bsUser.getBsUser());
                    bzBsEntity.setDzBsType(1);
                    bzBsEntity.setDzBsDate(oneDate);
                    bzBsEntity.setDzBsMoney(bsUser.getWithdrawal());
                    bzBsMapper.addBzBs(bzBsEntity);

                    return Result.SUCCESS(ResultEnum.Withdrawal_SUCCESS);
                }
            }


        }
    }

















}
